﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLVis.Model;
using System.IO;

namespace SQLVis.Presentation.ViewModel.Helpers
{
    static class LogHelper
    {
        public static PocGraph LoadSlowQueryLog(string sPath)
        {

            var graph = new PocGraph();

           FileStream fileStream = new FileStream(sPath, FileMode.Open);

           string line;

            using (StreamReader reader = new StreamReader(fileStream))
            {
                 line = reader.ReadToEnd();
            }

            string [] lineas = line.Split(';');


            int k = 0;
            foreach (var item in lineas)
            {
                k++;
                string[] partes = item.Split(new char[] {'\n' }, StringSplitOptions.RemoveEmptyEntries);
                if (partes.Length > 3)
                {
                    partes[0] = partes[1];
                    partes[1] = partes[2];
                    partes[2] = partes[3];
                }
                if (!(k <= 2) && partes.Length>=3)
                {

                    var v = new PocVertex("E_Table_" + k);//i.ToString(), (i * i == 0 ? 10 : i * i), i % 4);

                    v.SqlQuery = partes[2];
                    
                    string time_rows = partes[1];
                    string[] partes_time_rows = time_rows.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    v.QueryTime =System.Convert.ToInt32(partes_time_rows[2]);
                    v.LockTime = System.Convert.ToInt32(partes_time_rows[4]);
                    v.RowsSent = System.Convert.ToInt32(partes_time_rows[6]);
                    v.RowsExamined = System.Convert.ToInt32(partes_time_rows[8]);


                    string user_host = partes[0];
                    string[] partes_user_host = user_host.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    v.User = partes_user_host[2];
                    v.Host = partes_user_host[4];

                    graph.AddVertex(v);
                }
                
            }
           

            graph.AddEdge(new PocEdge("0to1", graph.Vertices.ElementAt(0), graph.Vertices.ElementAt(1)));
            graph.AddEdge(new PocEdge("1to2", graph.Vertices.ElementAt(1), graph.Vertices.ElementAt(2)));
            graph.AddEdge(new PocEdge("2to3", graph.Vertices.ElementAt(2), graph.Vertices.ElementAt(3)));
            graph.AddEdge(new PocEdge("2to4", graph.Vertices.ElementAt(2), graph.Vertices.ElementAt(4)));
            graph.AddEdge(new PocEdge("0to5", graph.Vertices.ElementAt(0), graph.Vertices.ElementAt(5)));
            graph.AddEdge(new PocEdge("1to7", graph.Vertices.ElementAt(1), graph.Vertices.ElementAt(7)));
            graph.AddEdge(new PocEdge("4to6", graph.Vertices.ElementAt(4), graph.Vertices.ElementAt(6)));
            graph.AddEdge(new PocEdge("4to6", graph.Vertices.ElementAt(4), graph.Vertices.ElementAt(6)));
            graph.AddEdge(new PocEdge("3to6", graph.Vertices.ElementAt(3), graph.Vertices.ElementAt(6)));
            graph.AddEdge(new PocEdge("6to7", graph.Vertices.ElementAt(6), graph.Vertices.ElementAt(7)));
            graph.AddEdge(new PocEdge("8to9", graph.Vertices.ElementAt(8), graph.Vertices.ElementAt(9)));
            graph.AddEdge(new PocEdge("4to6", graph.Vertices.ElementAt(6), graph.Vertices.ElementAt(19)));
            graph.AddEdge(new PocEdge("4to6", graph.Vertices.ElementAt(4), graph.Vertices.ElementAt(11)));
            graph.AddEdge(new PocEdge("4to6", graph.Vertices.ElementAt(11), graph.Vertices.ElementAt(20)));

            for (int j = 0; j < 24; j++)
            {
                if (j < 23)
                    graph.AddEdge(new PocEdge(j.ToString() + "to" + (j + 1).ToString(), graph.Vertices.ElementAt(j), graph.Vertices.ElementAt(j + 1)));
            }

            return graph; 


        }



    }
}
